package cn.edu.bjfu.practice.practice.manager.controller;

import cn.edu.bjfu.practice.practice.app.common.response.ErrorFromUser;
import cn.edu.bjfu.practice.practice.app.common.response.Response;
import cn.edu.bjfu.practice.practice.manager.pojo.domain.UserTable;
import cn.edu.bjfu.practice.practice.manager.pojo.query.LoginForm;
import cn.edu.bjfu.practice.practice.manager.pojo.query.AddTeacherForm;
import cn.edu.bjfu.practice.practice.manager.pojo.query.ModifyTeacherForm;
import cn.edu.bjfu.practice.practice.manager.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
public class ManagerController {

    @Autowired
    ManagerService managerService;
    @PostMapping("/api/login")
    public Response<Object> login(@Validated LoginForm loginForm, HttpSession session, HttpServletRequest request) {
        UserTable res = managerService.login(loginForm, session, request);
        if(res.getPassword() != null) {
            return Response.success(res);
        } else {
            return Response.error(ErrorFromUser.USER_PASS_ERROR);
        }
    }

    @PostMapping("/api/get_info")
    public Response<Object> get_info(HttpSession session) {
        return Response.success((UserTable)session.getAttribute("user"));
    }

    @PostMapping("/manager/add_teacher")
    public Response<Object> addTeacher(@Validated AddTeacherForm addTeacherForm) {
        managerService.addTeacher(addTeacherForm);
        return Response.message();
    }

    @PostMapping("/manager/modify_teacher")
    public Response<Object> modifyTeacher(@Validated ModifyTeacherForm form) {
        managerService.modifyTeacher(form);
        return Response.message();
    }

    @PostMapping("/manager/select_teacher")
    public Response<Object> selectTeacher() {
        return Response.success(managerService.selectTeacher());
    }
}
